package com.ruoyi.mentor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mentor.bo.RoundAddBo;
import com.ruoyi.mentor.bo.RoundEditBo;
import com.ruoyi.mentor.bo.RoundQueryBo;
import com.ruoyi.mentor.domain.Round;
import com.ruoyi.mentor.service.IRoundService;
import com.ruoyi.mentor.vo.RoundVo;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 志愿征集轮次信息Controller
 *
 * @author LH
 * @date 2021-05-21
 */
@Api(value = "志愿征集轮次信息控制器", tags = {"志愿征集轮次信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mentor/round")
public class RoundController extends BaseController {

    private final IRoundService iRoundService;

    /**
     * 查询志愿征集轮次信息列表
     */
    @ApiOperation("查询志愿征集轮次信息列表")
    @PreAuthorize("@ss.hasPermi('mentor:round:list')")
    @GetMapping("/list")
    public TableDataInfo<RoundVo> list(@Validated RoundQueryBo bo) {
        return iRoundService.queryPageList(bo);
    }

    /**
     * 导出志愿征集轮次信息列表
     */
    @ApiOperation("导出志愿征集轮次信息列表")
    @PreAuthorize("@ss.hasPermi('mentor:round:export')")
    @Log(title = "志愿征集轮次信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(@Validated RoundQueryBo bo) {
        List<RoundVo> list = iRoundService.queryList(bo);
        ExcelUtil<RoundVo> util = new ExcelUtil<RoundVo>(RoundVo.class);
        return util.exportExcel(list, "志愿征集轮次信息");
    }

    /**
     * 获取志愿征集轮次信息详细信息
     */
    @ApiOperation("获取志愿征集轮次信息详细信息")
    @PreAuthorize("@ss.hasPermi('mentor:round:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iRoundService.queryById(id));
    }

    /**
     * 新增志愿征集轮次信息
     */
    @ApiOperation("新增志愿征集轮次信息")
    @PreAuthorize("@ss.hasPermi('mentor:round:add')")
    @Log(title = "志愿征集轮次信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody RoundAddBo bo) {
        return toAjax(iRoundService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改志愿征集轮次信息
     */
    @ApiOperation("修改志愿征集轮次信息")
    @PreAuthorize("@ss.hasPermi('mentor:round:edit')")
    @Log(title = "志愿征集轮次信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody RoundEditBo bo) {
        return toAjax(iRoundService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除志愿征集轮次信息
     */
    @ApiOperation("删除志愿征集轮次信息")
    @PreAuthorize("@ss.hasPermi('mentor:round:remove')")
    @Log(title = "志愿征集轮次信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iRoundService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    /**
     * 查询当前时间是否处于最新的志愿选择时间
     */
    @ApiOperation("查询当前时间是否处于最新的志愿选择时间")
    @GetMapping("/isSelect/{type}")
    public AjaxResult isSelectTime(@PathVariable("type") Integer type) {
        RoundVo round = iRoundService.isSelectTime(type);
        if (round==null){
            return AjaxResult.success("当前系统未开放选择时间");
        }else {
            return AjaxResult.success("获取成功",round);
        }

    }

    /**
     * 设置当前轮次为激活状态
     */
    @ApiOperation("设置当前轮次为激活状态")
    @PostMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
        iRoundService.active(id);
        return AjaxResult.success();
    }

    /**
     * 取消激活状态
     */
    @ApiOperation("取消激活状态")
    @PostMapping("/noActive")
    public AjaxResult noActive() {
        iRoundService.noActive();
        return AjaxResult.success();
    }

    /**
     * 获取激活的时间段
     */
    @ApiOperation("获取激活的时间段")
    @GetMapping("/active")
    public AjaxResult active() {
        Round round = iRoundService.getOne(new QueryWrapper<Round>().eq("is_active", 1));
        if (round==null){
            return AjaxResult.success("当前系统未开放选择时间");
        }
        return AjaxResult.success(round);
    }


}
